<?php
/* MyMeeting is an online meeting management software.
 * Copyright (C) 2009 Government Of Malaysia
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * @author: Abdullah Zainul Abidin, Nuhaa All Bakry
 *          Eavay Javay Barnad, Sarogini Muniyandi, Teow Jit Huan
 *
 */

class Group extends AppModel {

/**
 * Defining the name of model
 *
 */
    var $name = 'Group';
/**
 * Defining the name of the table
 *
 */
    var $useTable = 'groups';


    //validation
/**
 * Validating the fields in group model
 *
 */
    var $validate = array(
        'name' => array(
            'required'=>array(
                'rule' => 'notEmpty'
            )
        )
    );     

/**
 * Building assosiations betweeen models
 *
 */
    var $belongsTo = array(
        'Committee' => array(
            'className' => 'Committee',
            'foreignKey' => 'committee_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );

    //The Associations below have been created with all possible keys, those that are not needed can be removed
/**
 * Building assosiations betweeen models
 *
 */
    var $hasAndBelongsToMany = array(
        'User' => array(
            'className' => 'User',
            'joinTable' => 'users_groups',
            'foreignKey' => 'group_id',
            'associationForeignKey' => 'user_id',
            'unique' => true,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'finderQuery' => '',
            'deleteQuery' => '',
            'insertQuery' => ''
        )
    );

/**
 * Finding all the related list of users in the group defined by the group_id
 * $param group_id finds all the related users in the group
 */
    function userList($group_id){
        $users=Set::extract($this->find('all',array('conditions'=>array('Group.id'=>$group_id))),'{n}.User.{n}.id');
        if(is_array($group_id)){
            return $users;
        }
        else{
            return $users[0];
        }
    }
}
?>
